<?php
/**
 * Hy - 系统管理员、管理组模块
 *
 * Copyright (c) 2013 YinHailin All rights reserved.
 * @Author: YinHailin
 * @Authorize: Boren Network
 * @Created by YinHailin at 2013-11-20
 */

defined('LY_VERSION') or exit();

class AdminMod extends PublicMod {

  /* 系统内置分类ID */
  static $SysClassId = '1';

  /* 系统内置数据ID */
  static $SysDataId = '1';

  /* 管理组列表 */
  public function GroupList() {
    parent::GetGlobal();  //调用公共方法
    parent::CheckLogin();  //检查登录
    parent::CheckPurview('admin');  //权限判断

    $objAdminGroup = M(self::$dataAdminGroup);

    $list = $objAdminGroup->Order('`sequence` asc, `id` asc')->Select();
    $list = deep_htmlspecialchars_decode($list);
    $this->Assign('list', $list); //数据循环变量
    //系统内置分类
    $this->Assign('sysClassId', self::$SysClassId);

    $this->Display();
  }

  /* 管理组排序 */
  public function GroupSequence() {
    parent::GetGlobal(); //调用全局方法
    parent::CheckLogin();  //检查登录
    parent::CheckPurview('admin');  //权限判断

    $objAdminGroup = M(self::$dataAdminGroup);

    if ($_POST) {
      $POST = deep_htmlspecialchars($_POST['sequence']);
      foreach ($POST as $key => $value) {
        $data['sequence'] = get_between($value);
        $objAdminGroup->Where('`id` = '.$key)->Save($data);
      }

      $backUrl = $_GET['url'];
      $this->Success('操作成功！', $backUrl);
    }
  }

  /* 管理组编辑 */
  public function GroupEdit() {
    parent::GetGlobal(); //调用全局方法
    parent::CheckLogin();  //检查登录
    parent::CheckPurview('admin');  //权限判断

    $objAdminGroup = M(self::$dataAdminGroup);

    if ($_POST) {
      $POST = deep_htmlspecialchars($_POST);
      $POST['id'] = intval($POST['id']);
      parent::CheckField('group_name', $POST['group_name'], 'require', '管理员组名称不能为空');
      parent::CheckField('group_name', $POST['group_name'], 'unique', '管理员组名称已经存在', self::$dataAdminGroup, 'id <>'.$POST['id']);
      $data['group_name'] = $POST['group_name'];
      $data['sequence'] = get_between($POST['sequence']);
      $data['group_purview'] =  implode(',',$POST['purview']);
      $objAdminGroup->Where('`id` ='.$POST['id'])->save($data);

      $backUrl = $_GET['url'];
      $this->Success('操作成功！', $backUrl);
    } else {
      $id = intval($_GET['id']);
      $data = $objAdminGroup->Where('`id` = '.$id)->Find();
      $data = deep_htmlspecialchars_decode($data);
      $this->Assign('data', $data);

      //标记管理员组权限
      $purview = C('ADMIN_PURVIEW');
      $purview2 = rabbet('purview_other', 'purview');
      $purview = array_merge($purview, $purview2);
      $purview = get_purview($data['group_purview'], $purview);
      $this->Assign('purview', $purview); //数据循环变量

      //系统内置分类
      $this->Assign('sysClassId', self::$SysClassId);

      $this->Display();
    }
  }

  /* 管理组添加 */
  public function GroupAdd() {
    parent::GetGlobal(); //调用全局方法
    parent::CheckLogin();  //检查登录
    parent::CheckPurview('admin');  //权限判断

    $objAdminGroup = M(self::$dataAdminGroup);

    if ($_POST) {
      $POST = deep_htmlspecialchars($_POST);
      parent::CheckField('group_name', $POST['group_name'], 'require', '管理员组名称不能为空');
      parent::CheckField('group_name', $POST['group_name'], 'unique', '管理员组名称已经存在', self::$dataAdminGroup);
      $data['group_name'] = $POST['group_name'];
      $data['sequence'] = get_between($POST['sequence']);
      $data['group_purview'] =  implode(',',$POST['purview']);
      $objAdminGroup->Add($data);

      $backUrl = $_GET['url'];
      $this->Success('操作成功！', $backUrl);
    } else {

      //标记管理员组权限
      $purview = C('ADMIN_PURVIEW');
      $purview2 = rabbet('purview_other', 'purview');
      $purview = array_merge($purview, $purview2);
      $purview = get_purview($data['group_purview'], $purview);
      $this->Assign('purview', $purview);  //数据循环变量

      $this->Display();
    }
  }

  /* 管理组删除 */
  public function GroupDelete() {
    parent::GetGlobal(); //调用全局方法
    parent::CheckLogin();  //检查登录
    parent::CheckPurview('admin');  //权限判断

    $objAdmin = M(self::$dataAdmin);
    $objAdminGroup = M(self::$dataAdminGroup);

    $id = intval($_GET['id']);
    $count = $objAdmin->Where('`group_id` = '.$id)->Count('id');

    $backUrl = $_GET['url'];
    if ($count > 0) {
      $this->Error('管理组中还有管理员存在，请移除管理员后再删除该管理组！', $backUrl);
    } else {
      $objAdminGroup->Where('`id` ='.$id)->Delete();
      $this->Success('操作成功！', $backUrl);
    }
  }

  /* 管理员列表 */
  public function AdminList() {
    parent::GetGlobal(); //调用全局方法
    parent::CheckLogin();  //检查登录
    parent::CheckPurview('admin');  //权限判断

    $objAdmin = M(self::$dataAdmin);
    $count = $objAdmin->count('id');  //计算总数
    $p = new HyPage($count, 10);
    $list = $objAdmin->Alias('AS d')->Field('d.*, s.group_name')->Join(get_real_table(self::$dataAdminGroup).' AS s ON d.group_id = s.id')->Limit($p->FirstRow . ',' . $p->ListRow)->Order('d.id asc')->Select();
    $list = deep_htmlspecialchars_decode($list);
    $page = $p->Show();  //分页的导航条的输出变量
    $this->Assign('page', $page);
    $this->Assign('list', $list); //数据循环变量
    //系统内置数据
    $this->Assign('sysDataId', self::$SysDataId);

    $this->Display();
  }

  /* 管理员添加 */
  public function AdminAdd() {
    parent::GetGlobal(); //调用全局方法
    parent::CheckLogin();  //检查登录
    parent::CheckPurview('admin');  //权限判断

    $objAdmin = M(self::$dataAdmin);
    $objAdminGroup = M(self::$dataAdminGroup);

    if ($_POST) {
      $POST = deep_htmlspecialchars($_POST);
      parent::CheckField('admin_name', $POST['admin_name'], 'require', '用户名不能为空');
      parent::CheckField('admin_name', $POST['admin_name'], 'unique', '用户名已经存在', self::$dataAdmin);
      parent::CheckField('password', $POST['password'], 'require', '登录密码不能为空');
      parent::CheckField('password', $POST['password'], 'uniform', '两次输入的密码不一致', $POST['repassword']);
      $data['admin_name'] = $POST['admin_name'];
      $data['password'] = get_password($POST['password']);
      $data['group_id'] = $POST['group_id'];
      $data['status'] = $POST['status'] ? '1' : '0';
      $data['reg_time'] = mktime();
      $objAdmin->Add($data);

      $backUrl = $_GET['url'];
      $this->Success('操作成功！', $backUrl);
    } else {
      //获取分类列表
      $sortList = $objAdminGroup->Select();
      $sortList = deep_htmlspecialchars_decode($sortList);
      $this->Assign('sortList', $sortList);

      $this->Display();
    }
  }

  /* 管理员编辑 */
  public function AdminEdit() {
    parent::GetGlobal(); //调用全局方法
    parent::CheckLogin();  //检查登录
    parent::CheckPurview('admin');  //权限判断

    $objAdmin = M(self::$dataAdmin);
    $objAdminGroup = M(self::$dataAdminGroup);

    if ($_POST) {
      $POST = deep_htmlspecialchars($_POST);
      $POST['id'] = intval($POST['id']);
      if ($POST['password'] != '') {
        parent::CheckField('password', $POST['password'], 'uniform', '两次输入的密码不一致', $POST['repassword']);
        $data['password'] = get_password($POST['password']);
        $data['active_token'] = get_password(time().rand(1000,9999));
      }
      $data['group_id'] = $POST['group_id'];
      $data['status'] = $POST['status'] ? '1' : '0';
      $objAdmin->Where('`id` ='.$POST['id'])->Save($data);

      $backUrl = $_GET['url'];
      $this->Success('操作成功！', $backUrl);
    } else {
      //根据ID获取数据
      $id = intval($_GET['id']);
      $data = $objAdmin->Where('`id` = '.$id)->Find();
      $data = deep_htmlspecialchars_decode($data);
      $this->Assign('data', $data);
      //获取分类列表
      $sortList = $objAdminGroup->select();
      $sortList = deep_htmlspecialchars_decode($sortList);
      $this->Assign('sortList', $sortList);
      //系统内置数据
      $this->Assign('sysDataId', self::$SysDataId);

      $this->Display();
    }
  }

  /* 管理员删除 */
  public function AdminDelete() {
    parent::GetGlobal(); //调用全局方法
    parent::CheckLogin();  //检查登录
    parent::CheckPurview('admin');  //权限判断

    $objAdmin = M(self::$dataAdmin);

    $id = intval($_GET['id']);
    $objAdmin->Where('`id` = '.$id)->Delete();

    $backUrl = $_GET['url'];
    $this->success('操作成功！', $backUrl);
  }
}